
/**
 *
 * @author Administrator
 */
import java.io.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class HtmlToText extends HTMLEditorKit.ParserCallback {

    StringBuffer s;

    // khoi tao
    public HtmlToText() {
    }

    //
    public void parse(Reader in) throws IOException {
        s = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        // the third parameter is TRUE to ignore charset directive
        delegator.parse(in, this, Boolean.TRUE);
    }

    public void handleText(char[] text, int pos) {
        s.append(text);
    }

    public String getText() {
        return s.toString();
    }

    public String readFile() {
        File file = new File("C:\\all.html");
        StringBuffer contents = new StringBuffer();
        try {
            BufferedReader input = new BufferedReader(new FileReader(file));
            try {
                String line = null;
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

//        System.out.println(contents.toString());
        return contents.toString();
    }

    //////////////////
    public static void main(String[] args) {
        try {
            // the HTML to convert
            HtmlToText RHTML = new HtmlToText();
           // RHTML.readFile();
            /////////////
            StringBuffer content = new StringBuffer();
            StringReader in = new StringReader(RHTML.readFile());

            Html2Text parser = new Html2Text();
            parser.parse(in);

            in.close();
            System.out.println(parser.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
